package com.zsk.controller;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.zsk.bean.TableParams;
import com.zsk.service.IJpaCodeGenService;
import com.zsk.util.MvnPrjUtil;
import com.zsk.util.ResponseUtil;

@RestController
public class JpaCodeGenController {

    @Autowired
    private IJpaCodeGenService jpaCodeGenService;

    @PostMapping("/api/gen/code")
    public void genCode(HttpServletRequest request, HttpServletResponse response, @RequestBody List<TableParams> list)
        throws Exception {
        String packageName = request.getParameter("packageName");
        String groupId = request.getParameter("groupId");
        String artifactId = request.getParameter("artifactId");
        // jpaCodeGenService.genCode(list, packageName,groupId,artifactId);
        jpaCodeGenService.genJpaPrj(response, list, packageName, groupId, artifactId);
    }

    @GetMapping("/api/gen/{name}")
    public void test(HttpServletResponse response, @PathVariable String name) {
        File prjZipFile = new File(MvnPrjUtil.workingDir + "/" + name + ".zip");
        ResponseUtil.writeFileToResponse(response, name + ".zip", prjZipFile);
    }

    @PostMapping("/api/gen/code/entity")
    public String genCodeEntity(HttpServletRequest request, @RequestBody TableParams tableInfo) {
        String packageName = request.getParameter("packageName");
        String artifactId = request.getParameter("artifactId");
        return jpaCodeGenService.genEntity(tableInfo, packageName, artifactId);
    }

    @PostMapping("/api/gen/code/repository")
    public String genCodeRepository(HttpServletRequest request, @RequestBody TableParams tableInfo) {
        String packageName = request.getParameter("packageName");
        String artifactId = request.getParameter("artifactId");
        return jpaCodeGenService.genRepository(tableInfo, packageName, artifactId);
    }

    @PostMapping("/api/gen/code/service")
    public String genCodeService(HttpServletRequest request, @RequestBody TableParams tableInfo) {
        String packageName = request.getParameter("packageName");
        String artifactId = request.getParameter("artifactId");
        return "\n===============================================itf===============================================\n"
            + jpaCodeGenService.genServiceItf(tableInfo, packageName, artifactId)
            + "\n===============================================impl===============================================\n"
            + jpaCodeGenService.genService(tableInfo, packageName, artifactId);
    }

}
